package com.examplemartin.jpademo.controller.common;


import com.examplemartin.jpademo.util.CpachaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/common")
@Slf4j
public class Cpacha {

    @GetMapping("/generate_cpacha")

    public void generateCpacha(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        CpachaUtil cpachaUtil = new CpachaUtil(4, 32, 120, 38);
        String vCode = cpachaUtil.generatorVCode();


        request.getSession().setAttribute("vcode", vCode.toUpperCase());
        log.warn("验证码=" + vCode.toUpperCase());


        ImageIO.write(cpachaUtil.generatorRotateVCodeImage(vCode, true), "gif", response.getOutputStream());
        return;
    }

}
